<!--
 * @Description:: 请补充填写
 * @Version: 1.0
 * @Author: 小白
 * @Date: 2022-04-14 14:26:46
 * @LastEditors: 小白
 * @LastEditTime: 2025-11-10 11:10:56
 * @FilePath: \tmsadmin\src\views\warehouse\components\list.vue
-->

<template>
  <SearchForm @search="searchFun" style="margin-bottom: 20px" />
  <el-card class="page_card" shadow="never">
    <template #header>
      <div class="card-header">
        <div class="left">
          仓库管理
          <template v-if="store.state.userInfo.houseId == 0">
             <el-button style="margin-left: 20px"  type="primary" @click="add('新增仓库')">
            新增仓库
          </el-button>
          <!-- 按钮组 -->
            <el-button type="success" @click="edit('编辑仓库')" style="margin-left: 20px">
              编辑仓库
            </el-button>
          <el-button type="danger" @click="showDelete('备注')" style="margin-left: 20px">
            删除仓库
          </el-button>
          </template>
        </div>
        <div class="right">
          <el-button circle @click="refreshFun">
            <i class="icon ion-refresh" :class="iconRefresh" />
          </el-button>
        </div>
      </div>
    </template>
    <Table :table-data="tableData" :table-columns="columns" :page-size="pageSize" :page-num="pageNum" :loading="loading"
      :total="total" @edit="edits" @upload="upload" @selectionChange="selectionChange" @change="changePage"
      @ok="getData()" />
    <Modal @ok="getData()" ref="modalRef"></Modal>
  </el-card>
</template>
<script lang="ts" setup>
import { REQUEST } from '@/api/request'
import { ElMessage, ElMessageBox} from 'element-plus'
import SearchForm from './search.vue'
import Table from './table.vue'
import Modal from './modal.vue'
import { onMounted, ref } from 'vue'
import { store } from '@/store'
const queryForm = ref({})
const iconRefresh = ref('')
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)
const modalRef = ref(null)
const loading = ref(false)
const tableData = ref([])
const columns = ref([
  {
    title: '仓库编码',
    prop: 'houseCode',
    align: 'center',
    modifiable: true,
    isShow: true,
    minwidth: '180'
  },
  {
    title: '仓库名称',
    prop: 'houseName',
    align: 'center',
    modifiable: true,
    isShow: true,
    minwidth: '180'
  },
  {
    title: '仓库地址',
    prop: 'address',
    align: 'center',
    modifiable: true,
    isShow: true,
    tooltip: false,
    minwidth: '280'
  },
  {
    title: '备注',
    prop: 'remark',
    align: 'center',
    modifiable: true,
    isShow: true,
    minwidth: '180'
  },
  {
    title: '是否启用',
    prop: 'isEnable',
    align: 'center',
    modifiable: true,
    isShow: true,
    minwidth: '180'
  },
  {
    title: '创建时间',
    prop: 'createTime',
    align: 'center',
    modifiable: true,
    isShow: true,
    minwidth: '180'
  },
  {
    title: '操作',
    prop: 'actions',
    align: 'center',
    fixed: 'right',
    minwidth: '250',
    modifiable: true,
    isShow: true,
  }
])
onMounted(() => {
  getData()
  console.log( store.state.userInfo,'00000')
})
const add = (title: string) => {
  modalRef.value.openModal(title)
}
const getData = () => {
  // loading.value = true
  REQUEST.POST('HOUSELIST', {
    houseId: store.state.userInfo.houseId>0?store.state.userInfo.houseId:'',
    pageNumber: pageNum.value,
    pageSize: pageSize.value,
    ...queryForm.value
  }).then((res: any) => {
    tableData.value = res.data.records
    total.value = JSON.parse(res.data.total)
  })
}
const modalRefYhq = ref()
const refreshFun = () => {
  iconRefresh.value = 'iconRefresh'
  getData()
  setTimeout(() => {
    iconRefresh.value = ''
  }, 1000)
}
const searchFun = (e: any) => {
  queryForm.value = e
  getData()
}
const selectList = ref([])
const selectionChange = (e: any) => {
  selectList.value = e
}
const edit = (row: any) => {
  if(selectList.value.length == 1){
    modalRef.value.openModal('编辑仓库', selectList.value[0])
  }else{
    ElMessage.error('请勾选一条数据进行编辑')
  }
}
const upload = (row: any) => {
  modalRef.value.openModal('上传公章', row)
}
const edits = (data: any) => {
  console.log(data.isEnable)
  REQUEST.POST('HOUSEEDIT', {
    id: data.id,
    isEnable: data.isEnable
  }).then((res: any) => {
    if (res.status === 200) {
      ElMessage({
        type: 'success',
        message: '操作成功'
      })
      getData()
    } else {
      ElMessage({
        type: 'warning',
        message: res.message
      })
    }
  })
}
const changePage = (e: any) => {
  pageNum.value = e.pageNum
  pageSize.value = e.pageSize
  getData()
}
const showDelete = (row: any) => {
  if(selectList.value.length > 0){
    ElMessageBox.confirm('您将进行删除操作，请确认', '提示', {
    distinguishCancelAndClose: true,
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(() => {
    const ids = selectList.value.map((item:any) => item.id)
    REQUEST.POST('HOUSEDEL', {
      ids: ids
    }).then((res: any) => {
      if (res.status === 200) {
        ElMessage({
          type: 'success',
          message: '成功删除'
        })
        getData()
        } else {
        ElMessage({
          type: 'warning',
          message: res.message
        })
      }
    })
  })
  }else{
    ElMessage({
      type: 'warning',
      message: '请勾选数据进行删除'
    })
  }
}
</script>

<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
